package model.preprocessor;

import java.io.BufferedWriter;
import java.io.IOException;

import controller.preprocessor.Hypre;
import controller.xml.XMLBaliseFormat;

/**
 * Class for managing of {@link Hypre} parameter.
 * 
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class HypreParameter extends PreProcessorParameter {

	/** value "not used" for a parameter */
	public static final int	NOT_USED		= -4;
	/** minimal value of verbosity parameter */
	public static final int	MIN_VERBOSITY	= 0;
	/** maximal value of verbosity parameter */
	public static final int	MAX_VERBOSITY	= 3;

	private int				verbosity		= NOT_USED;

	/**
	 * Export the data in XML
	 * 
	 * @param file
	 *            {@link BufferedWriter} to write
	 * @throws IOException
	 */
	@Override
	public void exportXML(BufferedWriter file) throws IOException {
		// open
		// file.newLine();
		file.write("<" + XMLBaliseFormat.PARAMETER + " " + XMLBaliseFormat.TYPE_PARAMETER + "=" + "\""
				+ XMLBaliseFormat.TYPE_PARAMETER_PREPROCESSOR + "\"" + " >");

		// preproc param
		super.exportXML(file);

		// verbosity
		// file.newLine();
		file.write("<" + XMLBaliseFormat.HYPRE_VERBOSITY + ">");
		file.write(String.valueOf(verbosity));
		file.write("</" + XMLBaliseFormat.HYPRE_VERBOSITY + ">");

		// close
		// file.newLine();
		file.write("</" + XMLBaliseFormat.PARAMETER + ">");
	}


	/**
	 * Return the verbosity.
	 * 
	 * @return the verbosity.
	 */
	public int getVerbosity() {
		return verbosity;
	}

	/**
	 * Set the verbosity parameter.
	 * 
	 * @param verbosity
	 *            new value.
	 * @return if it works.
	 */
	public boolean setVerbosity(int verbosity) {
		if (verbosity != NOT_USED && (verbosity > MAX_VERBOSITY || verbosity < MIN_VERBOSITY))
			return false;

		this.verbosity = verbosity;
		return true;
	}

	@Override
	public boolean basicCheck() {
		if (!super.basicCheck())
			return false;

		if (verbosity != NOT_USED && (verbosity > MAX_VERBOSITY || verbosity < MIN_VERBOSITY))
			return false;

		return true;
	}

	/**
	 * Create new parameter for {@link Hypre};
	 * 
	 * @param out
	 *            the output file.
	 */
	public HypreParameter(String out) {
		super(out);
	}

	/**
	 * Create new parameter for {@link Hypre};
	 */
	public HypreParameter() {
	}


	/**
	 * Create a new {@link HypreParameter} based on a {@link PreProcessorParameter}.
	 * 
	 * @param param
	 *            a {@link PreProcessorParameter}
	 */
	public HypreParameter(PreProcessorParameter param) {
		timeOut = param.getTimeOut();
		out = param.getOut();
	}


}
